import WujieVue from 'wujie-vue3'
import lifeCycles from './lifeCycle'
import hostMap from './hostMap'
import credentialsFetch from './fetch'
import router from '../router'
import type { App } from 'vue'
import type { RouteLocationNormalizedGeneric } from 'vue-router'

const { bus, setupApp } = WujieVue
bus.$on('click', msg => window.alert(msg))
// 在 xxx-sub 路由下子应用将激活路由同步给主应用，主应用跳转对应路由高亮菜单栏
bus.$on('sub-route-change', (to: RouteLocationNormalizedGeneric) => {
    const currentPath = router.currentRoute.value.path
    if (currentPath === to.path) return
    router.push(to)
})

const degrade =
    window.localStorage.getItem('degrade') === 'true' ||
    !window.Proxy ||
    !window.CustomElementRegistry
const props = {
    jump: name => {
        router.push({ name })
    },
}
const attrs = {} //isProduction ? { src: hostMap("//localhost:8000/") } : {};

setupApp({
    name: 'proa',
    url: hostMap('//localhost:4100/'),
    attrs,
    exec: true,
    alive: true,
    // sync: true,
    props,
    fetch: credentialsFetch,
    degrade,
    ...lifeCycles,
})
setupApp({
    name: 'prob',
    url: hostMap('//localhost:4200/'),
    attrs,
    exec: true,
    alive: true,
    // sync: true,
    props,
    fetch: credentialsFetch,
    degrade,
    ...lifeCycles,
})
export default {
    install: (app: App) => {
        app.use(WujieVue)
    },
}
